﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;

using FloatComparer = DDDTest.Helper.FloatComparer;

namespace DDDTest.Core {
    [TestClass]
    public class TestIndexBuffer {
        [TestMethod]
        public void Test_New_Point () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Point, 2);

            Assert.AreEqual (PrimitiveType.Point, ibuf.PrimitiveType);
            Assert.AreEqual (2, ibuf.PrimitiveCount);
            Assert.AreEqual (1, ibuf.Cpp);
            Assert.AreEqual (IndexType.Int, ibuf.IndexType);
            Assert.AreEqual (2, ibuf.IndexCount);
        }

        [TestMethod]
        public void Test_New_Line () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Line, 2);

            Assert.AreEqual (PrimitiveType.Line, ibuf.PrimitiveType);
            Assert.AreEqual (2, ibuf.PrimitiveCount);
            Assert.AreEqual (2, ibuf.Cpp);
            Assert.AreEqual (IndexType.Int, ibuf.IndexType);
            Assert.AreEqual (4, ibuf.IndexCount);
        }

        [TestMethod]
        public void Test_New_Triangle () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);

            Assert.AreEqual (PrimitiveType.Triangle, ibuf.PrimitiveType);
            Assert.AreEqual (2, ibuf.PrimitiveCount);
            Assert.AreEqual (3, ibuf.Cpp);
            Assert.AreEqual (IndexType.Int, ibuf.IndexType);
            Assert.AreEqual (6, ibuf.IndexCount);
        }

        [TestMethod]
        public void Test_New_Type () {
            var ibuf_byte = new IndexBuffer<byte> (PrimitiveType.Triangle, 2);
            var ibuf_sbyte = new IndexBuffer<sbyte> (PrimitiveType.Triangle, 2);
            var ibuf_short = new IndexBuffer<short> (PrimitiveType.Triangle, 2);
            var ibuf_ushort = new IndexBuffer<ushort> (PrimitiveType.Triangle, 2);
            var ibuf_int = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var ibuf_uint = new IndexBuffer<uint> (PrimitiveType.Triangle, 2);

            Assert.AreEqual (IndexType.Byte, ibuf_byte.IndexType);
            Assert.AreEqual (IndexType.SByte, ibuf_sbyte.IndexType);
            Assert.AreEqual (IndexType.Short, ibuf_short.IndexType);
            Assert.AreEqual (IndexType.UShort, ibuf_ushort.IndexType);
            Assert.AreEqual (IndexType.Int, ibuf_int.IndexType);
            Assert.AreEqual (IndexType.UInt, ibuf_uint.IndexType);
        }

        /// <summary>
        /// Get/Setの基本的なテスト
        /// </summary>
        [TestMethod]
        public void Test_GetSet_1 () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            var buf = new int[6];

            ibuf.Set (0, 2, indices);
            ibuf.Get (0, 2, buf);

            CollectionAssert.AreEqual (indices, buf);
        }

        /// <summary>
        ///  中途半端な位置のGet/Setのテスト
        /// </summary>
        [TestMethod]
        public void Test_GetSet_2 () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 10);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            var buf = new int[6];

            ibuf.Set (5, 2, indices);
            ibuf.Get (5, 2, buf);

            CollectionAssert.AreEqual (indices, buf);
        }

        /// <summary>
        /// 列挙子のテスト
        /// </summary>
        [TestMethod]
        public void Test_Indices () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            ibuf.Set (0, 2, indices);

            Assert.AreEqual (21, ibuf.Indices.Sum ());
        }

        /// <summary>
        /// 暗黙的な列挙子のテスト
        /// </summary>
        [TestMethod]
        public void Test_Enumerator () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            ibuf.Set (0, 2, indices);

            Assert.AreEqual (21, ibuf.Sum ());
        }
    }
}
